package com.lyouu.dataminer.util;

import java.util.ArrayList;

import com.lyouu.dataminer.model.TourCity;
import com.lyouu.dataminer.model.TourCountry;
import com.lyouu.dataminer.model.TourViewpoint;
import com.lyouu.dataminer.xmlhandler.TourMapXmlParser;

public class TourMap {
	private static TourMap instance = null;
	private ArrayList<TourCountry> countryList;
	private ArrayList<TourCity> cityList;

	public static TourMap getInstance() {
		if (instance == null) {
			synchronized (TourMap.class) {
				instance = new TourMap();
			}
		}
		return instance;
	}

	protected TourMap() {
		countryList = new ArrayList<TourCountry>();
		cityList = new ArrayList<TourCity>();
		TourMapXmlParser.parse(countryList, cityList);
	}

	public ArrayList<TourCountry> getTourCountryList() {
		return countryList;
	}

	public ArrayList<TourCity> getTourCityList() {
		return cityList;
	}

	public ArrayList<String> retrieveDepartPlace(String str) {
		ArrayList<String> departPlaceList = new ArrayList<String>();
		if (str != null) {
			if (str.contains("各地")) {
				departPlaceList.add(str);
			} else {
				for (TourCity c : getTourCityList()) {
					if (!c.getNameCn().isEmpty() && str.contains(c.getNameCn())
							|| !c.getNameEn().isEmpty()
							&& str.contains(c.getNameEn())
							|| !c.getNameOrig().isEmpty()
							&& str.contains(c.getNameOrig())) {
						departPlaceList.add(c.getNameCn());
					}
				}
				for (TourCountry c : getTourCountryList()) {
					if (!c.getNameCn().isEmpty() && str.contains(c.getNameCn())
							|| !c.getNameEn().isEmpty()
							&& str.contains(c.getNameEn())
							|| !c.getNameOrig().isEmpty()
							&& str.contains(c.getNameOrig())) {
						departPlaceList.add(c.getNameCn());
					}
				}
			}
		}
		return departPlaceList;
	}

	public String findDestCountries(String routine) {
		String destCountryStr = "";
		String[] routineAry = routine.split("-");
		ArrayList<String> destCountryList = new ArrayList<String>();
		ArrayList<TourViewpoint> viewpoints;
		boolean hasFound;
		for (int i = 0; i < routineAry.length; i++) {
			if (routineAry.length > 1) {
				if (i == 0 && routineAry[i].equals("德国")) // 德国是出发地，不添加到目的国家
					continue;
				if (i == routineAry.length - 1 && routineAry[i].equals("德国")) // 回到德国，不添加到目的国家
					break;
			}
			hasFound = false;
			for (TourCountry country : countryList) {
				if (routineAry[i].contains(country.getNameCn())) {
					if (!destCountryExists(country.getNameCn(), destCountryList))
						destCountryList.add(country.getNameCn());
					hasFound = true;
					break;
				}
			}
			if (!hasFound) {
				for (TourCity city : cityList) {
					if (routineAry[i].contains(city.getNameCn())) {
						if (!destCountryExists(city.getCountry().getNameCn(),
								destCountryList))
							destCountryList.add(city.getCountry().getNameCn());
						break;
					}
					viewpoints = (ArrayList<TourViewpoint>) city.getViewpoints();
					if (!viewpoints.isEmpty()) {
						for (TourViewpoint viewpoint : viewpoints) {
							if (routineAry[i].contains(viewpoint.getNameCn())) {
								if (!destCountryExists(city.getCountry()
										.getNameCn(), destCountryList))
									destCountryList.add(city.getCountry()
											.getNameCn());
								break;
							}
						}
					}
				}
			}
		}
		for (int i = 0; i < destCountryList.size() - 1; i++) {
			destCountryStr += destCountryList.get(i) + ",";
		}
		if (!destCountryList.isEmpty())
			destCountryStr += destCountryList.get(destCountryList.size() - 1);
		return destCountryStr;
	}

	private boolean destCountryExists(String destCountry,
			ArrayList<String> destCountryList) {
		boolean exists = false;
		for (int i = 0; i < destCountryList.size(); i++) {
			if (destCountry.equals(destCountryList.get(i))) {
				exists = true;
				break;
			}
		}
		return exists;
	}
}
